package cn.gene.timeyan.article.webapi.job;

import cn.gene.timeyan.article.webapi.mapper.ArticleMapper;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@EnableScheduling
public class ArticleJob implements Job {
    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;
    @Autowired
    private ArticleMapper articleMapper;

    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //获取所有缓存中文章详情的key
        Set<String> set=redisTemplate.keys("articleDetail:*");
        System.out.println(set);
        for (String key:set) {
            System.out.println(key);
            String articleId=key.substring(key.lastIndexOf(":")+1);
            //获取点赞数
            int like=(int)redisTemplate.opsForHash().get(key,"like");
            //获取转发数
            int retweeted=(int)redisTemplate.opsForHash().get(key,"retweeted");
            //获取浏览数
            int visit=(int)redisTemplate.opsForHash().get(key,"visit");
            //获取收藏数
            int collection=(int)redisTemplate.opsForHash().get(key,"collection");
            //获取评论数
            int comment=(int)redisTemplate.opsForHash().get(key,"comment");
            int line=articleMapper.updateArticleCountByArticleId(like,retweeted,visit,collection,comment,articleId);
        }
    }
}
